package com.cy.study.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cy.study.common.BasePageReq;
import com.cy.study.common.Response;
import com.cy.study.entity.NewsEntity;
import com.cy.study.req.NewsAddReq;
import com.cy.study.req.DeleteNewsReq;
import com.cy.study.req.NewsPageReq;
import com.cy.study.req.NewsUpdateReq;
import com.cy.study.res.NewsDetailRes;
import com.cy.study.res.NewsPageRes;
import com.cy.study.service.NewsService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * 资讯控制器
 */
@RequiredArgsConstructor
@RequestMapping("/news")
@RestController
@CrossOrigin
@Slf4j
public class NewsController {

    private final NewsService newsService;

    /**
     * 管理员新增资讯
     * @param req
     * @return
     */
    @PostMapping("/add")
    public Response<Integer> addNews(@RequestBody NewsAddReq req){
        Integer id = newsService.addNews(req);
        return Response.success(id);
    }

    /**
     * 管理员新增资讯
     * @param req
     * @return
     */
    @PostMapping("/update")
    public Response updateNews(@RequestBody NewsUpdateReq req){
        newsService.updateNews(req);
        return Response.success();
    }

    /**
     * 管理员删除资讯
     * @param req
     * @return
     */
    @PostMapping("/delete")
    public Response deleteNews(@RequestBody DeleteNewsReq req){
        newsService.deleteNews(req);
        return Response.success();
    }

    /**
     * 管理员资讯列表
     * @param req
     * @return
     */
    @PostMapping("/page")
    public Response<Page<NewsPageRes>> pageNews(@RequestBody NewsPageReq req){
        Page<NewsPageRes> res = newsService.pageNews(req);
        return Response.success(res);
    }

    /**
     * 资讯详情
     * @param newsId
     * @return
     */
    @GetMapping("/detail/{newsId}")
    public Response<NewsDetailRes> detail(@PathVariable Integer newsId){
        NewsDetailRes info = newsService.detail(newsId);
        return Response.success(info);
    }

    /**
     * 用户端资讯列表
     * @param req
     * @return
     */
    @PostMapping("/userList")
    public Response<Page<NewsEntity>> userList(@RequestBody BasePageReq req){
        Page<NewsEntity> res  = newsService.userList(req);
        return Response.success(res);
    }

}
